﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Extended Code Document Object Model
 * Version 2.10
 * 
 * AI Folder Class
 *      Represents and manages a folder and its contents within a project.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using System.Xml;
using System.ComponentModel;
using Adaptive.Foundation;
using Adaptive.Foundation.Collections;

namespace Adaptive.Foundation.CodeDom
{
    /// <summary>
    /// Represents and manages a folder and its contents within a project.
    /// </summary>
    public class AiFolder : AiObjectBase, IXmlStorable
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Description of the folder.
        /// </summary>
        private string _description;
        /// <summary>
        /// Valuie indicating whether the contents are in thier own namespace.
        /// </summary>
        private bool _indicatesNamespace;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AiFolder()
            : base()
        {
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="folderName">
        /// The name of the folder.
        /// </param>
        public AiFolder(string folderName)
            : base()
        {
            Name = folderName;
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="folderName">
        /// The name of the folder.
        /// </param>
        /// <param name="description">
        /// An optional description of the folder.
        /// </param>
        /// <param name="indicatesNamespace">
        /// A value indicating whether the contents are contained in thier own namespace.
        /// </param>
        public AiFolder(string folderName, string description, bool indicatesNamespace)
            : base()
        {
            Name = folderName;
            _description = description;
            _indicatesNamespace = indicatesNamespace;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
            }
            _description = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the description of the folder.
        /// </summary>
        /// <value>
        /// Optional description text.
        /// </value>
        [Browsable(true), DefaultValue(""), Description("A user description of the folder.")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        /// <summary>
        /// Gets or sets the description of the folder.
        /// </summary>
        /// <value>
        /// Optional description text.
        /// </value>
        [Browsable(true), DefaultValue(false), Description("Determines whether the contents are in their own sub-namespace.")]
        public bool IndicatesNamespace
        {
            get { return _indicatesNamespace; }
            set { _indicatesNamespace = value; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            _description = string.Empty;
            _indicatesNamespace = false;
        }
        /// <summary>
        /// Attempts to save the specific object instance to an open stream.
        /// </summary>
        /// <param name="writer">
        /// An open <see cref="XmlWriter"/> instance used to write the content
        /// to the open stream.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool Save(XmlWriter writer)
        {
            bool returnValue = true;

            try
            {
                writer.WriteStartElement("AiFolder");
                writer.WriteAttributeString("name", Name);
                writer.WriteAttributeString("description", _description);
                writer.WriteAttributeString("indicatesNamespace", _indicatesNamespace.ToString());
                writer.WriteStartElement("items");
            }
            catch
            {
                returnValue = false;
            }

            if (returnValue)
            {
                try
                {
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                catch
                {
                    returnValue = false;
                }
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to load the specific object content from an open stream.
        /// </summary>
        /// <param name="reader">
        /// An open <see cref="XmlReader"/> instance used to reader content
        /// from the open stream.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool Load(XmlReader reader)
        {
            return false;
        }
        /// <summary>
        /// Returns an XML representation of the current object instance.
        /// </summary>
        /// <returns>
        /// XML Code representing the current object instance.
        /// </returns>
        public string ToXml()
        {
            StringBuilder builder = null;

            builder = new StringBuilder();
            builder.Append("<AiFolder name=\"" + Name + "\" description=\"" + _description +
                         " indicatesNamespace=" + _indicatesNamespace.ToString() + "\">\r\n");

            //Add projects.
            builder.Append("\t<items>\r\n");
/*            _folders.MoveFirst();
            while (!_folders.Eof)
            {
                builder.Append("\t\t" + _folders.Value.ToXml());
                _folders.MoveNext();
            }*/
            builder.Append("\t</items>\r\n");
            builder.Append("</AiFolder>\r\n");

            return builder.ToString();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
